package stark.app.function.markdown;

import static android.app.Activity.RESULT_OK;

import androidx.annotation.Nullable;

import android.content.Intent;

import com.blankj.utilcode.util.ToastUtils;
import com.stark.doc.selector.DefDocSelActivity;
import com.stark.markdownconverter.lib.MdConvertUtils;

import java.io.IOException;
import java.util.ArrayList;

import io.reactivex.rxjava3.core.ObservableEmitter;
import stark.app.R;
import stark.app.databinding.FragmentMarkdownDemoBinding;
import stark.common.basic.base.BaseNoModelFragment;
import stark.common.basic.constant.Extra;
import stark.common.basic.media.MediaInfo;
import stark.common.basic.utils.RxUtil;
import stark.common.basic.utils.StkPermissionHelper;
import stark.common.basic.utils.WorkPathUtil;

public class MarkdownDemoFragment extends BaseNoModelFragment<FragmentMarkdownDemoBinding> {

    private static final int FUNC_MD_TO_PDF = 1;
    private static final int FUNC_MD_TO_WORD = 2;

    private static final int REQ_MD_TO_PDF = 1;
    private static final int REQ_MD_TO_WORD = 2;

    @Override
    protected int onCreate() {
        return R.layout.fragment_markdown_demo;
    }

    @Override
    protected void initView() {
        mDataBinding.btnMdConvertPdf.setOnClickListener(v -> {
            reqPermission(FUNC_MD_TO_PDF);
        });
        mDataBinding.btnMdConvertWord.setOnClickListener(v -> {
            reqPermission(FUNC_MD_TO_WORD);
        });
    }

    @Override
    protected void initData() {

    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode != RESULT_OK || data == null) return;
        MediaInfo mediaInfo = (MediaInfo) data.getSerializableExtra(Extra.DATA);
        String path = mediaInfo.getPath();
        if (requestCode == REQ_MD_TO_PDF) {
            mdConvertPdf(path);
        } else if (requestCode == REQ_MD_TO_WORD) {
            mdConvertWord(path);
        }
    }

    private void reqPermission(int func) {
        StkPermissionHelper.reqManageExternalStoragePermission().callback(new StkPermissionHelper.ACallback() {
            @Override
            public void onGranted() {
                ArrayList<String> typeList = new ArrayList<>();
                typeList.add("text/markdown");
                switch (func) {
                    case FUNC_MD_TO_PDF:
                        DefDocSelActivity.selFile(MarkdownDemoFragment.this, typeList, REQ_MD_TO_PDF);
                        break;
                    case FUNC_MD_TO_WORD:
                        DefDocSelActivity.selFile(MarkdownDemoFragment.this, typeList, REQ_MD_TO_WORD);
                        break;
                }
            }
        }).request();
    }

    private void mdConvertPdf(String mdPath) {
        showDialog("保存中...");
        RxUtil.create(new RxUtil.Callback<String>() {
            @Override
            public void doBackground(ObservableEmitter<String> emitter) {
                String saveFilePath = WorkPathUtil.generatePdfFilePath();
                try {
                    MdConvertUtils.convertToPdf(mdPath, saveFilePath);
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
                emitter.onNext(saveFilePath);
            }

            @Override
            public void accept(String path) {
                dismissDialog();
                ToastUtils.showShort("转换成功");
            }
        });
    }

    private void mdConvertWord(String mdPath) {
        showDialog("保存中...");
        RxUtil.create(new RxUtil.Callback<String>() {
            @Override
            public void doBackground(ObservableEmitter<String> emitter) {
                String saveFilePath = WorkPathUtil.generateWordFilePath();
                try {
                    MdConvertUtils.convertToWord(mdPath, saveFilePath);
                } catch (IOException e) {

                }
                emitter.onNext(saveFilePath);
            }

            @Override
            public void accept(String path) {
                dismissDialog();
                ToastUtils.showShort("转换成功");
            }
        });
    }

}